﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace KFS_Inventory_CigInput
{
    public partial class createAccount : Form
    {
        public createAccount()
        {
            InitializeComponent();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void createButton_Click(object sender, EventArgs e)
        {
            string username = userBox.Text;
            string password = pwBox.Text;
            string repeatpw = pwBox2.Text;
            if (password == repeatpw)
            {
                string firstName = firstBox.Text;
                string lastName = lastBox.Text;
                string email = emailBox.Text;
                string phone = phoneBox.Text;
                string storeAccess = accessCombo.Text;
                string security = roleCombo.Text;
                // Open new window with manager authentication
                using (var authForm = new managerAuth())
                {
                    var result = authForm.ShowDialog();

                        if (authForm.ReturnValue1)
                        {
                            // Connect to db and insert new record
                            var mongoConnect = "mongodb://root:b713eb8b@ds029811.mongolab.com:29811/kreiser_inventory";
                            MongoClient client = new MongoClient(mongoConnect);
                            MongoServer server = client.GetServer();
                            MongoDatabase kfs_inv = server.GetDatabase("kreiser_inventory");
                            var userCollection = kfs_inv.GetCollection<BsonDocument>("UserInfo");

                            var userQuery = Query.EQ("username", username);
                            var fields = Fields.Include("_id");
                            var res = userCollection.Find(userQuery).SetFields(fields).SetLimit(1).FirstOrDefault();
                            if (res != null)
                            {
                                // A match was found
                                MessageBox.Show("The username has already been taken. Please pick a different one.");
                            }
                            else
                            {
                                BsonDocument userEntry = new BsonDocument {
                                {"username", username},
                                {"password", password},
                                {"firstName", firstName},
                                {"lastName", lastName},
                                {"email", email},
                                {"phone", phone},
                                {"storeAccess", storeAccess},
                                {"security", security}};
                                userCollection.Insert(userEntry);
                                MessageBox.Show("User " + username + " has been successfully authenticated.");
                                this.Close();
                            }


                        }
                        else
                        {
                            MessageBox.Show("The entry has not been authorized");
                        }
                }

            }
            else
            {
                MessageBox.Show("The passwords do not match.");
            }
        }
    }
}
